;
(function () {
  window.assVue = new Vue({
    el: '#assgn-create',
    data: {
      flow: {
        active: 'CT',
        activeName: '合同流程',
        list: []
      },
      isDefault: {
        active: 'Y',
        list: [{
          name: '是',
          code: 'Y'
        }, {
          name: '否',
          code: 'N'
        }]
      },
      status: {
        active: 'Y',
        list: [{
          name: '启用',
          code: 'Y'
        }, {
          name: '停用',
          code: 'N'
        }]
      },
      memo: '',
      nodeFlag: false,
      showNode: [],
      nodeIndex: 0,
      tenantProjectFlag: false,
      allcheckProjectflag: false,
      tenantProjectData: [],
      partProject: [],
      queryProjects: [],
      flowId: hlUrlUtil.GetQueryString('id'),
      flowKey: hlUrlUtil.GetQueryString('bpmKey'),

      signData: [{
        name: '会签',
        code: 'Y'
      }, {
        name: '串签',
        code: 'N'
      }],


    },
    methods: {
      handleClick(val) {
        this.showNode[this.nodeIndex].signType = [val[0].code]
      },
      //添加节点
      newAddNode(i) {
        let flag = false
        this.showNode.map(item => {
          if (item.flag) {
            flag = true
          }
        })
        if (flag) {
          hl.toast('请先保存当前审批节点')
          return
        }
        let data = {
          name: '',
          type: 'P',
          flag: true,
          user: []
        }
        data.signType = ['N']
        this.showNode.unshift(data)
        approve.data.selectPerson = [];
        approve.data.getCount = false
        approve.methods.detailList(approve.data.initId, $('.approve-bottom'), true)
        this.nodeIndex = 0
      },
      //保存编辑的节点
      saveNode(i) {
        if (this.showNode[i].type == 'B') {
          if (!this.showNode[i].name) {
            hl.toast('默认节点名不能为空')
            return
          }
        }
        this.showNode[i].flag = false
      },
      cancelNode(i) {
        if (this.showNode[i].type == 'P') {
          if (!this.showNode[i].name) {
            this.showNode.splice(i, 1)
            this.nodeIndex = i
          }
        }
        if (this.showNode[i].type == 'B') {
          if (!this.showNode[i].name) {
            hl.toast('默认节点名不能为空')
            return
          }
        }
        this.showNode[i].flag = false
      },
      //编辑节点
      editNode(i) {
        this.showNode[i].flag = true
      },
      //删除添加的节点
      delNode(i) {
        this.showNode.splice(i, 1)
        this.nodeIndex = i
      },
      //流程切换
      flowTab(item) {
        console.log(item)
        approve.data.getCount = false
        this.showNode = JSON.parse(item.dictProperty);
        let setData = JSON.parse(item.dictProperty);
        setData.map(single => {
          single.signType = single.signType || ['N']
        })
        this.showNode = JSON.parse(JSON.stringify(setData))
        approve.data.selectPerson = [];
        approve.methods.detailList(approve.data.initId, $('.approve-bottom'), true)
        this.flow.active = item.code;
        this.flow.activeName = item.name;
        this.nodeIndex = 0;
      },
      //节点导航切换
      nodeTab(i) {
        let flag = false;
        this.showNode.map(item => {
          if (item.flag) {
            flag = true
          }
        })
        if (flag) {
          hl.toast('请先保存当前审批节点')
          return
        }
        this.nodeIndex = i;
        approve.data.selectPerson = this.showNode[i].user;
        approve.data.getCount = false
        approve.methods.detailList(approve.data.initId, $('.approve-bottom'), true)
      },
      //是否默认
      isDefaultEvent(code) {
        this.isDefault.active = code;
        if (code == 'Y') {
          this.nodeFlag = false;
        }
        if (code == 'N') {
          this.nodeFlag = true;
        }
        this.partProject = []
        this.queryProjects = []
      },
      //获取租户所有项目
      tenantProject() {
        this.tenantProjectFlag = true;
        hlhttp.get({
          url: 'tenant/fn_projects',
          data: {
            config_state: '#'
          },
          success: (data) => {
            data.map(item => {
              item.flag = false
            })
            data.map(item => {
              this.partProject.map(item0 => {
                if (item.projectId == item0.projectId) {
                  item.flag = true
                }
              })
            })
            this.tenantProjectData = data
          }
        })
      },
      //全选不选项目
      allcheckProject() {
        this.allcheckProjectflag = !this.allcheckProjectflag;
        if (this.allcheckProjectflag) {
          this.tenantProjectData.map(item => {
            item.flag = true
          })
          this.partProject = JSON.parse(JSON.stringify(this.tenantProjectData))
        }
        if (!this.allcheckProjectflag) {
          this.tenantProjectData.map(item => {
            item.flag = false
            this.partProject = []
          })
        }
      },
      //单选操作
      singleCheck(i) {
        if (this.tenantProjectData[i].flag) {
          this.tenantProjectData[i].flag = false;
          this.partProject.map((item, index) => {
            if (item.projectId == this.tenantProjectData[i].projectId) {
              this.partProject.splice(index, 1)
            }
          })
          this.allcheckProjectflag = false
          return
        }
        if (!this.tenantProjectData[i].flag) {
          this.tenantProjectData[i].flag = true;
          this.partProject.push(this.tenantProjectData[i])
        }
      },
      //删除操作
      delCheck(i) {
        this.tenantProjectData.map(item => {
          if (item.projectId == this.partProject[i].projectId) {
            item.flag = false
          }
        })
        this.partProject.splice(i, 1)
        this.allcheckProjectflag = false
      },
      //确认选中项目
      queryProject() {
        this.queryProjects = JSON.parse(JSON.stringify(this.partProject))
        this.tenantProjectFlag = false
      },
      //获取流程当前版本,并提交流程
      getflowVersion() {
        console.log(this.showNode)
        console.log(this.showNode)
        let flag = false;
        let nodeName = [],
          nodeCode = [],
          userIds = [],
          userNames = [],
          projectIds = [];
        signTypes = []
        this.showNode.map(item => {
          if (!item.user.length) {
            flag = true
          }
          nodeName.push(item.name)
          nodeCode.push(item.type)
          signTypes.push(item.signType)
        })
        if (flag) {
          hl.toast('请添加配置人')
          return
        }
        this.showNode.map((item, index) => {
          userIds.push([])
          userNames.push([])
          item.user.map((item0) => {
            userIds[index].push(item0.id)
            userNames[index].push(item0.name)
          })
        })
        this.queryProjects.map(item => {
          projectIds.push(item.projectId)
        })
        if (this.isDefault.active == 'N') {
          if (!projectIds.length) {
            hl.toast('请选择项目')
            return
          }
        }
        if (userIds.length > 1) {
          userIds = userIds.join('#')
          userNames = userNames.join('#')
        }
        if (userIds.length <= 1) {
          userIds = userIds.join(',')
          userNames = userNames.join(',')
        }

        let data = {
          bpm_key: this.flow.active,
          bpm_name: this.flow.activeName,
          valid: this.status.active,
          is_default: this.isDefault.active,
          memo: this.memo || ' ',
          project_ids: projectIds.toString() || '#',
          phase_names: nodeName.toString(),
          phase_types: nodeCode.toString(),
          counter_sign_flags: signTypes + '' || '#',
          user_ids: userIds,
          user_names: userNames
        }
        console.log(data)

        hlhttp.get({
          url: 'bpm/top_version',
          data: {
            bpm_key: this.flow.active
          },
          success: (res) => {
            hllayer.confirm1('本操作将发布合同流程的第' + (Number((res.topVersion || '0')) + 1) + '版本，并替换第' + (res.topVersion || '0') + '版本成为新的默认流程！', () => {
              hlhttp.post({
                url: 'bpm/bpdef',
                data: data,
                success: () => {
                  hl.toast('流程发布成功')
                  hlUrlUtil.goTo('flowConfig', {
                    ddtab: true
                  })
                }
              })
            })
          }
        })
      },
      //获取流程详情
      getFlowInfo() {
        hlhttp.get({
          url: 'bpm/bpdef_info',
          data: {
            bpdef_id: this.flowId
          },
          success: (data) => {
            this.isDefault.active = data.isDefault
            if (data.isDefault == 'N') {
              this.nodeFlag = true;
              let projectNames = data.projectNames ? data.projectNames.split(',') : [];
              let projectIds = data.projectIds ? data.projectIds.split(',') : [];
              projectNames.map((item, index) => {
                this.partProject.push({
                  projectName: item,
                  projectId: projectIds[index]
                })
                this.queryProjects.push({
                  projectName: item,
                  projectId: projectIds[index]
                })
              })
            }
            this.showNode = [];
            data.bpdefPhase.map(item => {
              item.user = []
              item.userIds.map((item0, index0) => {
                item.user.push({
                  id: item0,
                  name: item.userNames[index0]
                })
              })
            })
            data.bpdefPhase.map(item => {
              this.showNode.push({
                name: item.name,
                type: item.type,
                flag: false,
                user: item.user,
                signType: item.counterSignFlag? [item.counterSignFlag] : ['N']
              })
            })
            approve.data.selectPerson = this.showNode[this.nodeIndex].user;
            this.flow.active = data.bpmKey;
            this.flow.activeName = data.bpmName;
            this.memo = data.memo;
            this.status.active = data.valid;
            this.$refs.flowname.innerHTML = data.bpmName;
          }
        })
      },
      //获取流程列表
      getFlowList() {
        hlhttp.get({
          url: 'configure/dict',
          data: {
            module_code: 'BPM',
            config_name: 'BPM_CONFIG_TYPE'
          },
          success: (data => {
            let newData = []
            let userinfo = JSON.parse(localStorage.getItem('userMes'))
            newData = hlArrayUtil.replaceData(data, [{
              key: 'dictDisplayName',
              rKey: 'name'
            }, {
              key: 'dictValue',
              rKey: 'code'
            }])
            if (userinfo.preSysCode.indexOf('SINOOCEAN') == -1) {
              newData.map((item, index) => {
                if (item.dictValue == 'RRAT') {
                  newData.splice(index, 1)
                }
              })
            }
            this.flow.active = newData[0].code
            this.flow.activeName = newData[0].name
            this.flow.list = newData
            let setData = JSON.parse(newData[0].dictProperty)

            setData.map(item => {
              item.signType = item.signType || ['N']
            })

            this.showNode = JSON.parse(JSON.stringify(setData))

            if (this.flowId) {
              this.getFlowInfo()
            }
          })
        })
      }
    },
    created() {
      hlSelectUi()
      this.getFlowList()
    },
    mounted() {
      approve.dom.addinitialDom()
      approve.event.wxddNodeEvent((data) => {
        data = hlArrayUtil.removeRepeat(data, 'id');
        this.showNode[this.nodeIndex].user = data;
      })

    }
  })
}())